import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import nightwatchPlugin from 'vite-plugin-nightwatch'
import { resolve } from 'node:path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { VantResolver } from '@vant/auto-import-resolver'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: true,
    port: 9006,
    proxy: {
      '/parkos-gateway': {
        target: 'https://szyqbzdev.net4iot.com:30002',
        changeOrigin: true
      },
      '/parkos-sso': {
        target: 'https://szyqbzdev.net4iot.com:30002',
        changeOrigin: true
      }
    }
  },
  envPrefix: 'VUE_',
  plugins: [
    vue(),
    vueJsx(),
    nightwatchPlugin(),
    AutoImport({
      // 按需自动导入 Vue 的 API。具有 TypeScript 支持。
      imports: ['vue', 'vue-router'],
      resolvers: [ElementPlusResolver()],
      dts: 'auto-imports.d.ts' // 生成文件的位置
    }),
    Components({
      // Vue的按需组件自动导入
      // 默认导入组件目录
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass'
        }),
        VantResolver()
      ],
      dts: 'components.d.ts' // 生成文件的位置
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      '/#': resolve(__dirname, 'types')
    }
  },
  build: {
    outDir: 'dist',
    target: 'es2015',
    // 库模式 https://cn.vitejs.dev/guide/build#library-mode
    lib: {
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'workflow',
      formats: ['es', 'cjs', 'iife'],
      fileName: (format) => `workflow.${format}.js`
    },
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: ['vue', 'element-plus', 'vant', 'admin-ui-store'],
      output: {
        extend: true,
        // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
        globals: {
          vue: 'Vue',
          'element-plus': 'ElementPlus',
          vant: 'Vant'
        }
      }
    }
  }
})
